package grpcserver

import (
	"context"
	pb "gitee.com/go-caipu-team/go-caipu-plugin-sdk/genproto/pluginmain"
	"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
	"go-caipu/pkg/infra/log"
	user "go-caipu/pkg/services/admin/sysuser"
	"go-caipu/pkg/setting"
)

var (
	_ pb.DataServer = &adminServer{}
)

type AdminService struct {
}
type adminServer struct {
	pb.UnimplementedDataServer
	cfg         *setting.Cfg
	adminServer *adminServer
	logger      log.Logger
	userService user.Service
}

func ProvideAdminService(cfg *setting.Cfg, grpcServerProvider Provider, userService user.Service) (*AdminService, error) {
	as := &adminServer{
		cfg:         cfg,
		logger:      log.New("GRpcServer"),
		userService: userService,
	}
	pb.RegisterDataServer(grpcServerProvider.GetServer(), as)
	return &AdminService{}, nil
}
func (as *adminServer) QueryData(ctx context.Context, req *pb.QueryDataRequest) (*pb.QueryDataResponse, error) {
	logger.Debug("QueryData", "request", req)
	//create response struct
	responses := pb.QueryDataResponse{
		Responses: make(map[string]*pb.DataResponse),
	}
	for _, q := range req.Queries {
		var qm queryModel
		qm.Command = q.Command
		logger.Debug("QueryData", "command", q.Command, "Json", q.Body)

		result := as.query(ctx, qm)
		responses.Responses[q.RefId] = &result
	}
	return &responses, nil
}

func (as *adminServer) CallResource(ctx context.Context, req *pb.CallResourceRequest) (*pb.CallResourceResponse, error) {
	//
	return &pb.CallResourceResponse{}, nil
}
